package com.xiaoshuidi.cloud.module.iot.service.impl.thirdPartImpl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.tuya.connector.api.exceptions.ConnectorResultException;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.iot.controller.admin.doorlock.vo.OperateThirdDeviceRequestModel;
import com.xiaoshuidi.cloud.module.iot.controller.admin.doorlock.vo.OperateThirdDeviceResponseModel;
import com.xiaoshuidi.cloud.module.iot.controller.admin.doorlock.vo.PullThirdDeviceRequestModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.AccessTokenRequestModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.AccessTokenResponseModel;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDeviceDo;
import com.xiaoshuidi.cloud.module.iot.dal.repository.DeviceRepository;
import com.xiaoshuidi.cloud.module.iot.dal.repository.TbAccountRepository;
import com.xiaoshuidi.cloud.module.iot.enums.BrandEnum;
import com.xiaoshuidi.cloud.module.iot.enums.DeviceTypeEnum;
import com.xiaoshuidi.cloud.module.iot.infrastructure.remote.TuYaDeviceConnector;
import com.xiaoshuidi.cloud.module.iot.infrastructure.remote.dto.*;
import com.xiaoshuidi.cloud.module.iot.service.AllAbstractThirdInstanceService;
import com.xiaoshuidi.cloud.module.iot.util.AESUtil;
import com.xiaoshuidi.cloud.module.system.api.sms.SmsSendApi;
import com.xiaoshuidi.cloud.module.system.api.sms.dto.send.SmsSendSingleToUserReqDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ：baohanzhong
 * @date ：Created 2024/8/21 16:24
 * @description：涂鸦对接门锁实例
 */
@SuppressWarnings("all")
@Service("tuYaInstanceServiceImpl")
public class TuYaInstanceServiceImpl extends AllAbstractThirdInstanceService {

    private final static Logger LOGGER = LoggerFactory.getLogger(TuYaInstanceServiceImpl.class);

    @Resource
    private SmsSendApi smsFeignService;

    @Autowired
    private TuYaDeviceConnector tuYaDeviceConnector;

    @Autowired
    private TbAccountRepository tbAccountDataAccess;

    @Autowired
    private DeviceRepository deviceDataAccess;

    @Value("${tuya.uid:ay1716262766359d1lXW}")
    private String UID = "UID";

    @Value("${connector.sk:9500d5d83a7c4e618bc6a235d6129e56}")
    private String secret = "SECRET";


    @Override
    public String getBrand() {
        return BrandEnum.TUYA.getBrand();
    }

    @Override
    public AccessTokenResponseModel getAccessToken(AccessTokenRequestModel accessTokenRequestModel) throws Exception {
        AccessTokenResponseModel accessTokenResponseModel = new AccessTokenResponseModel();
        accessTokenResponseModel.setAccessToken("NONE");
        //由于直接集成了sdk,这里不需要获取access_token,但是统一入口需要提供，那就返回UID作为access_token
        accessTokenResponseModel.setExpireTime(60*60*24*30L);//有效期30天，其实是永久有效
        return accessTokenResponseModel;
    }

    @Override
    public List<TbDeviceDo> getDevice(PullThirdDeviceRequestModel pullThirdDeviceRequestModel) throws Exception {
        List<TbDeviceDo> tbDeviceDos = new ArrayList<>();
        String loginName = pullThirdDeviceRequestModel.getLoginName();
        if(StrUtil.isNotBlank(loginName)){
            int pageNum = 1;
            int pageSize = 100;
            Boolean hasMore = Boolean.FALSE;
            List<SmartLockDevices.RecordsDTO> allRecords = new ArrayList<>();
            String lastRowKey = "";
            do {
                Map<String,Object> params=new HashMap<>();
                params.put("last_row_key", lastRowKey);
                params.put("size",pageSize);
                try {
                    SmartLockDevices smartLockDevices = tuYaDeviceConnector.getDevices(params);
                    hasMore = smartLockDevices.getHas_more();
                    List<SmartLockDevices.RecordsDTO> records = smartLockDevices.getDevices();
                    if(CollectionUtil.isNotEmpty(records)){
                        //records过滤出来门锁，其他的全部不需要
                        records=records.stream().filter(recordsDTO -> recordsDTO.getCategory().equals("ms")).collect(Collectors.toList());
                        //处理设备数据
                        allRecords.addAll(records);
                    }
                    lastRowKey = smartLockDevices.getLast_row_key();
                }catch (ServiceException e){
                    LOGGER.error("获取设备列表失败："+e.getMessage());
                }
            } while (hasMore);
            LOGGER.info("获取设备列表返回结果："+ JSONUtil.toJsonStr(allRecords));
            if(CollectionUtil.isNotEmpty(allRecords)){
                //开始处理设备数据
                List<TbDeviceDo> existDevices = deviceDataAccess.getDeviceListByOneApiNo(pullThirdDeviceRequestModel.getTbAccountDo().getApiNo());
                List<String> existDevicesUuid = existDevices.stream().map(existDevice -> existDevice.getDeviceThirdNo()).collect(Collectors.toList());
                //拉取过得设别就过滤掉
                allRecords=allRecords.stream().filter(recordsDTO -> !existDevicesUuid.contains(recordsDTO.getId())).collect(Collectors.toList());
                for (SmartLockDevices.RecordsDTO recordsDTO : allRecords) {
                    //循环处理数据
                    TbDeviceDo tbDeviceDo = new TbDeviceDo();
                    tbDeviceDo.setDeviceThirdNo(recordsDTO.getId());
                    tbDeviceDo.setDeviceName(recordsDTO.getName());
                    tbDeviceDo.setDeviceModel(recordsDTO.getModel());
                    tbDeviceDo.setUpdateTime(LocalDateTime.now());
                    tbDeviceDo.setReportTime(LocalDateTime.now());
                    tbDeviceDo.setOnlineStatus(recordsDTO.getOnline()?1:0);
                    tbDeviceDo.setBatteryLevel(100D);//没有默认100%电量
                    tbDeviceDo.setDeviceType(DeviceTypeEnum.DOORLOCK.getCode());
                    tbDeviceDos.add(tbDeviceDo);
                }
            }
        }
        return tbDeviceDos;
    }

    @Override
    public OperateThirdDeviceResponseModel addDoorLockPassword(OperateThirdDeviceRequestModel operateThirdDeviceRequestModel) throws Exception {
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = new OperateThirdDeviceResponseModel();
        operateThirdDeviceResponseModel.setOperateResult(false);
        //1在线密码 6离线密码，由于对接需求只有在线密码，所以不处理离线密码，但是接口也对接了
        operateThirdDeviceResponseModel = addDoorLockZxPassword(operateThirdDeviceRequestModel);
        Long passwordStartTime = operateThirdDeviceRequestModel.getPasswordStartTime();
        Date date = new Date(passwordStartTime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String beginTime = sdf.format(date);

        Long passwordEndTime = operateThirdDeviceRequestModel.getPasswordEndTime();
        Date date1 = new Date(passwordEndTime);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String endTime = sdf1.format(date1);

        Map<String, Object> templateParams = Maps.newHashMap();
        templateParams.put("password",operateThirdDeviceRequestModel.getPasswordContent());
        templateParams.put("begin",beginTime);
        templateParams.put("end",endTime);
        SmsSendSingleToUserReqDTO dto=new SmsSendSingleToUserReqDTO();
        dto.setTemplateParams(templateParams);
        if(operateThirdDeviceRequestModel.getGate() == null || !operateThirdDeviceRequestModel.getGate()) {
            dto.setTemplateCode("user-sms-lockpassword");
        }else {
            dto.setTemplateCode("user-sms-lockpassword2");
        }
        dto.setMobile(operateThirdDeviceRequestModel.getMobile());
        CommonResult<Long> longCommonResult = smsFeignService.sendSingleSmsToMember(dto);
        LOGGER.info("下发密码发送短信返回结果："+ JSONUtil.toJsonStr(longCommonResult));
        //保留代码
//        if ("1".equals(operateThirdDeviceRequestModel.getPasswordType())) {
//
//        } else if ("6".equals(operateThirdDeviceRequestModel.getPasswordType())) {
//            operateThirdDeviceResponseModel = addDoorLockLxPassword(operateThirdDeviceRequestModel);
//        }
        return operateThirdDeviceResponseModel;
    }

    private OperateThirdDeviceResponseModel addDoorLockLxPassword(OperateThirdDeviceRequestModel operateThirdDeviceRequestModel) {
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = new OperateThirdDeviceResponseModel();
        operateThirdDeviceResponseModel.setOperateResult(Boolean.FALSE);
        //由于涂鸦直接封装了sdk，这里直接调用sdk的接口即可
        String deviceThirdNo = operateThirdDeviceRequestModel.getDeviceThirdNo();
        GetOfflineTempPasswordReqDTO getOfflineTempPasswordReqDTO = new GetOfflineTempPasswordReqDTO();
        getOfflineTempPasswordReqDTO.setType(0);
        getOfflineTempPasswordReqDTO.setName(operateThirdDeviceRequestModel.getMobile());
        getOfflineTempPasswordReqDTO.setEffective_time(operateThirdDeviceRequestModel.getPasswordStartTime()/1000);
        getOfflineTempPasswordReqDTO.setEffective_time(operateThirdDeviceRequestModel.getPasswordEndTime()/1000);
        OfflineTempPasswordDTO offlineTempPassword = tuYaDeviceConnector.getOfflineTempPassword(deviceThirdNo, getOfflineTempPasswordReqDTO);
        if (Objects.nonNull(offlineTempPassword)) {
            String tempPassword = offlineTempPassword.getOffline_temp_password();
            operateThirdDeviceResponseModel.setThirdPassId(tempPassword);
            operateThirdDeviceRequestModel.setPasswordContent(tempPassword);
            operateThirdDeviceResponseModel.setPasswordStatus(1);
            operateThirdDeviceResponseModel.setOperateResult(Boolean.TRUE);
        }else{
            operateThirdDeviceResponseModel.setPasswordStatus(5);
        }
        return operateThirdDeviceResponseModel;
    }

    private OperateThirdDeviceResponseModel addDoorLockZxPassword(OperateThirdDeviceRequestModel operateThirdDeviceRequestModel) {
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = new OperateThirdDeviceResponseModel();
        operateThirdDeviceResponseModel.setOperateResult(Boolean.FALSE);
        //由于涂鸦直接封装了sdk，这里直接调用sdk的接口即可
        String deviceThirdNo = operateThirdDeviceRequestModel.getDeviceThirdNo();
        PasswordTicketDTO passwordTicket = tuYaDeviceConnector.getPasswordTicket(deviceThirdNo);
        LOGGER.info("获取密码凭证返回结果：{}", JSONUtil.toJsonStr(passwordTicket));
        if (Objects.nonNull(passwordTicket)) {
            Integer expireTime = passwordTicket.getExpire_time();
            if(Objects.nonNull(expireTime)&&expireTime>0){
                String ticketId = passwordTicket.getTicket_id();
                String ticketKey = passwordTicket.getTicket_key();
                GetTempPasswordReqDTO getTempPasswordReqDTO = new GetTempPasswordReqDTO();
                getTempPasswordReqDTO.setTicket_id(ticketId);
                getTempPasswordReqDTO.setEffective_time(operateThirdDeviceRequestModel.getPasswordStartTime()/1000);
                getTempPasswordReqDTO.setInvalid_time(operateThirdDeviceRequestModel.getPasswordEndTime()/1000);
                getTempPasswordReqDTO.setPassword_type("ticket");
                if(StrUtil.isBlank(operateThirdDeviceRequestModel.getUserName())){
                    getTempPasswordReqDTO.setName(operateThirdDeviceRequestModel.getMobile());
                }else{
                    getTempPasswordReqDTO.setName(operateThirdDeviceRequestModel.getUserName());
                }
                getTempPasswordReqDTO.setPhone(operateThirdDeviceRequestModel.getMobile());
                //原始密码进行处理
                String passwordContent = operateThirdDeviceRequestModel.getPasswordContent();
                String decrypt = AESUtil.decrypt(secret,ticketKey);
                String originalPassword = AESUtil.encrypt(decrypt,passwordContent );
                if(StrUtil.isBlank(originalPassword)){
                    operateThirdDeviceResponseModel.setPasswordStatus(5);
                    return operateThirdDeviceResponseModel;
                }
                getTempPasswordReqDTO.setPassword(originalPassword);
                LOGGER.info("原始数据：{}",JSONUtil.toJsonStr(getTempPasswordReqDTO));
                try {
                    TempPasswordDTO tempPassword = tuYaDeviceConnector.getTempPassword(deviceThirdNo, getTempPasswordReqDTO);
                    LOGGER.info("获取临时密码返回结果：{}",JSONUtil.toJsonStr(tempPassword));
                    if (Objects.nonNull(tempPassword)) {
                        Long id = tempPassword.getId();
                        operateThirdDeviceResponseModel.setOperateResult(Boolean.TRUE);
                        operateThirdDeviceResponseModel.setThirdPassId(String.valueOf(id));
                        //开始操作密码同步
                        try {
                            IssuePasswordReqDTO issuePasswordReqDTO = new IssuePasswordReqDTO();
                            issuePasswordReqDTO.setPassword_id(Long.valueOf(id));
                            Boolean b = tuYaDeviceConnector.issuePassword(deviceThirdNo, issuePasswordReqDTO);
                            LOGGER.info("密码同步结果：{}",b);
                            if (b){
                                //查询密码信息
                                GetTempPasswordInfoDTO tempPasswordInfo = tuYaDeviceConnector.getTempPasswordInfo(deviceThirdNo, String.valueOf(id));
                                if (Objects.nonNull(tempPasswordInfo)) {
                                    Integer phase = tempPasswordInfo.getPhase();
                                    Integer deliveryStatus = tempPasswordInfo.getDelivery_status();
                                    if(1==phase){
                                        operateThirdDeviceResponseModel.setPasswordStatus(4);
                                    }else if(2==phase){
                                        //正常
                                        operateThirdDeviceResponseModel.setPasswordStatus(1);
                                    }else if(3==phase){
                                        //冻结
                                        operateThirdDeviceResponseModel.setPasswordStatus(0);
                                    }else if(4==phase){
                                        //已删除
                                        operateThirdDeviceResponseModel.setPasswordStatus(5);
                                    }else if(5==phase){
                                        //创建失败
                                        operateThirdDeviceResponseModel.setPasswordStatus(5);
                                    }else{
                                        operateThirdDeviceResponseModel.setPasswordStatus(5);
                                    }
                                }
                            }else{
                                //密码同步失败
                                operateThirdDeviceResponseModel.setPasswordStatus(5);
                            }
                        }catch (Exception e){
                            LOGGER.error("密码同步失败，{}",id,e);
                            operateThirdDeviceResponseModel.setPasswordStatus(5);
                        }
                    }
                }catch (ServiceException e){
                    operateThirdDeviceResponseModel.setPasswordStatus(5);
                    operateThirdDeviceResponseModel.setOperateResult(Boolean.FALSE);
                    LOGGER.error("获取临时密码失败，{}",e.getMessage());
                }
            }
        }
        return operateThirdDeviceResponseModel;
    }

    @Override
    public OperateThirdDeviceResponseModel getDynamicDoorLockPassword(OperateThirdDeviceRequestModel operateThirdDeviceRequestModel) throws Exception {
        return super.getDynamicDoorLockPassword(operateThirdDeviceRequestModel);
    }

    @Override
    public OperateThirdDeviceResponseModel updateDoorLockPassword(OperateThirdDeviceRequestModel operateThirdDeviceRequestModel) throws Exception {
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = new OperateThirdDeviceResponseModel();
        operateThirdDeviceResponseModel.setOperateResult(Boolean.FALSE);
        //由于涂鸦直接封装了sdk，这里直接调用sdk的接口即可
        String deviceThirdNo = operateThirdDeviceRequestModel.getDeviceThirdNo();
        String passwordId = operateThirdDeviceRequestModel.getPasswordId();
        PasswordTicketDTO passwordTicket = tuYaDeviceConnector.getPasswordTicket(deviceThirdNo);
        LOGGER.info("获取密码凭证返回结果：{}", JSONUtil.toJsonStr(passwordTicket));
        if (Objects.nonNull(passwordTicket)) {
            String ticketKey = passwordTicket.getTicket_key();
            String ticketId = passwordTicket.getTicket_id();
            Integer expireTime = passwordTicket.getExpire_time();
            if(Objects.nonNull(expireTime)&&expireTime>0){
                ModifyTempPasswordReqDTO modifyTempPasswordReqDTO = new ModifyTempPasswordReqDTO();
                modifyTempPasswordReqDTO.setPassword_type("ticket");
                String passwordContent = operateThirdDeviceRequestModel.getPasswordContent();
                String decrypt = AESUtil.decrypt(secret,ticketKey);
                String originalPassword = AESUtil.encrypt(decrypt,passwordContent );
                if(StrUtil.isBlank(originalPassword)){
                    operateThirdDeviceResponseModel.setPasswordStatus(5);
                    return operateThirdDeviceResponseModel;
                }
                modifyTempPasswordReqDTO.setPassword(originalPassword);
                modifyTempPasswordReqDTO.setPhone(operateThirdDeviceRequestModel.getMobile());
                modifyTempPasswordReqDTO.setTicket_id(ticketId);
                modifyTempPasswordReqDTO.setEffective_time(operateThirdDeviceRequestModel.getPasswordStartTime()/1000);
                modifyTempPasswordReqDTO.setInvalid_time(operateThirdDeviceRequestModel.getPasswordEndTime()/1000);
                LOGGER.info("修改临时密码请求参数：deviceThirdNo：{},passwordId:{},modifyTempPasswordReqDTO:{}",deviceThirdNo,passwordId,JSONUtil.toJsonStr(modifyTempPasswordReqDTO));
                Boolean b = tuYaDeviceConnector.modifyTempPassword(deviceThirdNo, passwordId, modifyTempPasswordReqDTO);
                LOGGER.info("修改临时密码返回结果：{}",b);
                if (b){
                    operateThirdDeviceResponseModel.setOperateResult(Boolean.TRUE);
                    operateThirdDeviceResponseModel.setPasswordStatus(1);
                }else{
                    operateThirdDeviceResponseModel.setOperateResult(Boolean.FALSE);
                }
            }
        }
        return operateThirdDeviceResponseModel;
    }

    @Override
    public OperateThirdDeviceResponseModel frozenDoorLockPassword(OperateThirdDeviceRequestModel operateThirdDeviceRequestModel) throws Exception {
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = new OperateThirdDeviceResponseModel();
        operateThirdDeviceResponseModel.setOperateResult(Boolean.FALSE);
        String deviceThirdNo = operateThirdDeviceRequestModel.getDeviceThirdNo();
        String passwordId = operateThirdDeviceRequestModel.getPasswordId();
        Boolean b = tuYaDeviceConnector.freezePassword(deviceThirdNo, passwordId);
        LOGGER.info("冻结密码返回结果：{}",b);
        if (b){
            operateThirdDeviceResponseModel.setOperateResult(Boolean.TRUE);
            operateThirdDeviceResponseModel.setPasswordStatus(1);
        }else{
            operateThirdDeviceResponseModel.setOperateResult(Boolean.FALSE);
        }
        return operateThirdDeviceResponseModel;
    }

    @Override
    public OperateThirdDeviceResponseModel unfrozenDoorLockPassword(OperateThirdDeviceRequestModel operateThirdDeviceRequestModel) throws Exception {
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = new OperateThirdDeviceResponseModel();
        operateThirdDeviceResponseModel.setOperateResult(Boolean.FALSE);
        String deviceThirdNo = operateThirdDeviceRequestModel.getDeviceThirdNo();
        String passwordId = operateThirdDeviceRequestModel.getPasswordId();
        Boolean b = tuYaDeviceConnector.unfreezePassword(deviceThirdNo, passwordId);
        LOGGER.info("解冻密码返回结果：{}",b);
        if(b){
            operateThirdDeviceResponseModel.setOperateResult(Boolean.TRUE);
            operateThirdDeviceResponseModel.setPasswordStatus(1);
        }else{
            operateThirdDeviceResponseModel.setOperateResult(Boolean.FALSE);
        }
        return operateThirdDeviceResponseModel;
    }

    @Override
    public OperateThirdDeviceResponseModel deleteDoorLockPassword(OperateThirdDeviceRequestModel operateThirdDeviceRequestModel) throws Exception {
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = new OperateThirdDeviceResponseModel();
        operateThirdDeviceResponseModel.setOperateResult(Boolean.FALSE);
        String deviceThirdNo = operateThirdDeviceRequestModel.getDeviceThirdNo();
        String passwordId = operateThirdDeviceRequestModel.getPasswordId();
        Boolean b = tuYaDeviceConnector.deleteTempPassword(deviceThirdNo, passwordId);
        LOGGER.info("删除密码返回结果：{}",b);
        if(b){
            operateThirdDeviceResponseModel.setOperateResult(Boolean.TRUE);
            operateThirdDeviceResponseModel.setPasswordStatus(1);
        }else{
            operateThirdDeviceResponseModel.setOperateResult(Boolean.FALSE);
        }
        return operateThirdDeviceResponseModel;
    }

    @Override
    public OperateThirdDeviceResponseModel getDoorLockStatus(OperateThirdDeviceRequestModel operateThirdDeviceRequestModel) throws Exception {
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = new OperateThirdDeviceResponseModel();
        operateThirdDeviceResponseModel.setOperateResult(Boolean.FALSE);
        String deviceThirdNo = operateThirdDeviceRequestModel.getDeviceThirdNo();
        if(StrUtil.isNotBlank(deviceThirdNo)){
            DeviceDTO deviceInfo = tuYaDeviceConnector.getDeviceInfo(deviceThirdNo);
            LOGGER.info("{}获取门锁状态：{}",deviceThirdNo,JSONUtil.toJsonStr(deviceInfo));
            if (Objects.nonNull(deviceInfo)) {
                operateThirdDeviceResponseModel.setOnlineStatus(deviceInfo.getOnline() ? 1 : 0);
                operateThirdDeviceResponseModel.setBatteryLevel(100D);
                operateThirdDeviceResponseModel.setOperateResult(Boolean.TRUE);
            }
        }
        return operateThirdDeviceResponseModel;
    }

    @Override
    public OperateThirdDeviceResponseModel getOpenLockRecordes(OperateThirdDeviceRequestModel operateThirdDeviceRequestModel) throws Exception {
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = new OperateThirdDeviceResponseModel();
        operateThirdDeviceResponseModel.setOperateResult(false);
        String deviceThirdNo = operateThirdDeviceRequestModel.getDeviceThirdNo();
        Integer pageSize = operateThirdDeviceRequestModel.getPageSize();
        Integer pageNo = operateThirdDeviceRequestModel.getPageNo();
        Long queryBeginTime = operateThirdDeviceRequestModel.getQueryBeginTime();
        Long queryEndTime = operateThirdDeviceRequestModel.getQueryEndTime();
        GetOpenLogsReqDTO openLogsReqDTO = new GetOpenLogsReqDTO();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page_size", pageSize);
        params.put("page_no", pageNo);
        params.put("start_time", queryBeginTime);
        params.put("end_time", queryEndTime);
        LOGGER.info("获取开门记录请求参数：{}",JSONUtil.toJsonStr(params));
        OpenLogsDTO openLogs = tuYaDeviceConnector.getOpenLogs(deviceThirdNo, params);
        LOGGER.info("获取开门记录返回结果：{}",JSONUtil.toJsonStr(openLogs));
        if (Objects.nonNull(openLogs)) {
            operateThirdDeviceResponseModel.setOperateResult(Boolean.TRUE);
            List<OpenLogsDTO.LogsDTO> logs = openLogs.getLogs();
            if(CollectionUtil.isNotEmpty(logs)){
                JSONArray jsonArray = new JSONArray();
                for (OpenLogsDTO.LogsDTO log : logs) {
                    cn.hutool.json.JSONObject obj = new cn.hutool.json.JSONObject();
                    OpenLogsDTO.LogsDTO.StatusDTO status = log.getStatus();
                    String value = status.getValue();
                    obj.put("passwordId", value);
                    String code = status.getCode();
                    obj.put("openType", code);
                    obj.put("openTime",log.getUpdate_time());
                    jsonArray.add(obj);
                }
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("json", jsonArray);
                operateThirdDeviceResponseModel.setDataJson(jsonObject);
            }
        }
        return operateThirdDeviceResponseModel;
    }
}
